package comp512.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class ClientGUI_CreateGame extends JFrame {

	private JPanel contentPane;
	private JTextField txtServerAddress;
	private JTextField txtSessionID;
	private JTextField txtFolderLocation;

	/**
	 * Create the frame.
	 */
	public ClientGUI_CreateGame() {
		setMaximumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setMinimumSize(new Dimension(400, 200));
		setTitle("Create Game Session");
		setType(Type.UTILITY);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 324, 203);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);
		
		JLabel lblServerAddress = new JLabel("Server Address");
		sl_panel.putConstraint(SpringLayout.WEST, lblServerAddress, 10, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, lblServerAddress, -127, SpringLayout.SOUTH, panel);
		lblServerAddress.setBorder(new EmptyBorder(5, 5, 5, 5));
		panel.add(lblServerAddress);
		
		JLabel lblSessionId = new JLabel("Session ID");
		sl_panel.putConstraint(SpringLayout.WEST, lblSessionId, 10, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, lblSessionId, -20, SpringLayout.EAST, lblServerAddress);
		lblSessionId.setBorder(new EmptyBorder(5, 5, 5, 5));
		panel.add(lblSessionId);
		
		JLabel lblNumberOfInvites = new JLabel("Total Invites");
		sl_panel.putConstraint(SpringLayout.WEST, lblNumberOfInvites, 0, SpringLayout.WEST, lblServerAddress);
		sl_panel.putConstraint(SpringLayout.EAST, lblNumberOfInvites, 0, SpringLayout.EAST, lblServerAddress);
		lblNumberOfInvites.setPreferredSize(new Dimension(50, 14));
		lblNumberOfInvites.setBorder(new EmptyBorder(5, 5, 5, 5));
		panel.add(lblNumberOfInvites);
		
		JLabel lblSaveInvitesTo = new JLabel("Save invites to");
		sl_panel.putConstraint(SpringLayout.WEST, lblSaveInvitesTo, 0, SpringLayout.WEST, lblServerAddress);
		lblSaveInvitesTo.setBorder(new EmptyBorder(5, 5, 5, 5));
		panel.add(lblSaveInvitesTo);
		
		txtServerAddress = new JTextField();
		sl_panel.putConstraint(SpringLayout.EAST, lblServerAddress, -26, SpringLayout.WEST, txtServerAddress);
		sl_panel.putConstraint(SpringLayout.NORTH, txtServerAddress, 2, SpringLayout.NORTH, lblServerAddress);
		panel.add(txtServerAddress);
		txtServerAddress.setColumns(10);
		
		txtSessionID = new JTextField();
		sl_panel.putConstraint(SpringLayout.NORTH, lblSessionId, -2, SpringLayout.NORTH, txtSessionID);
		sl_panel.putConstraint(SpringLayout.NORTH, txtSessionID, 5, SpringLayout.SOUTH, txtServerAddress);
		sl_panel.putConstraint(SpringLayout.WEST, txtServerAddress, 0, SpringLayout.WEST, txtSessionID);
		sl_panel.putConstraint(SpringLayout.EAST, txtServerAddress, 0, SpringLayout.EAST, txtSessionID);
		sl_panel.putConstraint(SpringLayout.WEST, txtSessionID, 160, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, txtSessionID, -3, SpringLayout.EAST, panel);
		txtSessionID.setEnabled(false);
		panel.add(txtSessionID);
		txtSessionID.setColumns(10);
		
		txtFolderLocation = new JTextField();
		sl_panel.putConstraint(SpringLayout.WEST, txtFolderLocation, 160, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.NORTH, lblSaveInvitesTo, -2, SpringLayout.NORTH, txtFolderLocation);
		sl_panel.putConstraint(SpringLayout.EAST, lblSaveInvitesTo, -26, SpringLayout.WEST, txtFolderLocation);
		txtFolderLocation.setEditable(false);
		panel.add(txtFolderLocation);
		txtFolderLocation.setColumns(10);
		
		JButton btnBrowse = new JButton("");
		ImageIcon imgIconBrowse = new ImageIcon(ServerGUI_QuestionBank.class.getResource("/comp512/resources/Browse.png"));
		Image imgBrowse = imgIconBrowse.getImage();
		Image fixedSizeBrowse = imgBrowse.getScaledInstance(18, 18, Image.SCALE_SMOOTH);
		imgIconBrowse = new ImageIcon(fixedSizeBrowse);
		btnBrowse.setIcon(imgIconBrowse);
		sl_panel.putConstraint(SpringLayout.EAST, txtFolderLocation, -3, SpringLayout.WEST, btnBrowse);
		sl_panel.putConstraint(SpringLayout.NORTH, btnBrowse, 2, SpringLayout.NORTH, lblSaveInvitesTo);
		sl_panel.putConstraint(SpringLayout.WEST, btnBrowse, 346, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, btnBrowse, 0, SpringLayout.EAST, txtServerAddress);
		btnBrowse.setPreferredSize(new Dimension(20, 20));
		btnBrowse.setMaximumSize(new Dimension(2, 20));
		btnBrowse.setMinimumSize(new Dimension(20, 20));
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new java.io.File("."));
				chooser.setDialogTitle("Save Invites To...");
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				chooser.setAcceptAllFileFilterUsed(false);
				if(chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
					txtFolderLocation.setText(chooser.getSelectedFile().getAbsoluteFile().toString());
				}
			}
		});
		panel.add(btnBrowse);
		
		JButton btnGenerate = new JButton("Generate");
		sl_panel.putConstraint(SpringLayout.NORTH, btnGenerate, 118, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, btnGenerate, -10, SpringLayout.SOUTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, btnGenerate, 111, SpringLayout.WEST, panel);
		panel.add(btnGenerate);
		
		JSpinner spinner = new JSpinner();
		sl_panel.putConstraint(SpringLayout.WEST, spinner, 26, SpringLayout.EAST, lblNumberOfInvites);
		sl_panel.putConstraint(SpringLayout.EAST, spinner, -172, SpringLayout.EAST, panel);
		sl_panel.putConstraint(SpringLayout.NORTH, txtFolderLocation, 5, SpringLayout.SOUTH, spinner);
		sl_panel.putConstraint(SpringLayout.NORTH, spinner, 4, SpringLayout.SOUTH, txtSessionID);
		sl_panel.putConstraint(SpringLayout.NORTH, lblNumberOfInvites, 3, SpringLayout.NORTH, spinner);
		panel.add(spinner);
	}
}
